<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author:kane < chengjin005@163.com>
// +----------------------------------------------------------------------
namespace app\know\controller;

use app\know\model\ChapterModel;
use app\know\controller\AdminKnowBaseController;
use app\know\validate\ChapterValidate;

/**
 * Class AdminTeachingMaterialVerifyController 教材审核记录控制器
 * @package app\know\controller
 */
class AdminTeachingMaterialVerifyController extends AdminKnowBaseController
{
	
    public function index()
    {
        $content = hook_one('know_admin_teaching_material_verify_index_view');

        if (!empty($content)) {
            return $content;
        }
		$type = $this->request->param('type', 'SUBJECT');
		$this->assign('type', $type);
		
        return $this->fetch();
    }
	
    public function list()
    {
        $type = $this->request->param('type', 'SUBJECT');
        $pageNumber = $this->request->param('pageNumber', 1, 'intval');
        $pageSize = $this->request->param('pageSize', 10, 'intval');

        $where = ['type' => $type, 'is_delete' => 0 ];
        $count = SysConfigModel::where($where)->count();

        $rows = SysConfigModel::where($where)
                                    ->order('order', 'desc')
                                    ->page($pageNumber, $pageSize)
                                    ->select();
        
        $this->success('', null, ['rows' => $rows, 'total' => $count]);
    }
    
	public function add()
    {
        $param = $this->request->param();

        $user = $this->getAdminUser();
        $param['create_user'] = $user['user_login'];
        $param['uuid'] = $this->uuid('', '');
        
        try {
            $this->validateFailError($param, 'AdminSysConfig.add');
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }

        $row = SysConfigModel::create($param);
        if ($row) {
            $this->opLog([
                'table_name' => 'know_sysconfig',
                'sql' => SysConfigModel::getLastSql(),
                'create_user' => $user['user_login'],
            ]);
            $this->success('添加成功');
        }
        $this->error('添加失败');  
    }
	
	public function delete()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try {
            $this->validateFailError($param, 'AdminSysConfig.delete');
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try {
            SysConfigModel::where('id', 'in', preg_split("/[\s;,]/", $id))->save(['is_delete' => 1]);
            $this->opLog([
                'table_name' => 'know_sysconfig',
                'sql' => SysConfigModel::getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($id),
            ]);
        } catch (\Throwable $th) {
            $this->error($th->getMessage());
        }
        $this->success('已删除');
    }
	
	public function edit()
    {
        $param = $this->request->param();
        $id = $this->request->param('id', '');
        
        try {
            $this->validateFailError($param, 'AdminSysConfig.edit');
        } catch (ValidateException $e) {
            $this->result('', 0, $e->getError());
        }

        $user = $this->getAdminUser();
        $param['modify_user'] = $user['user_login'];

        try {
            $d = SysConfigModel::find($id);
            SysConfigModel::where('id', $id)->save($param);
            $this->opLog([
                'table_name' => 'know_sysconfig',
                'sql' => SysConfigModel::getLastSql(),
                'create_user' => $user['user_login'],
                'before_data' => json_encode($d),
            ]);
        } catch (\Throwable $th) {
            $this->error($th->getMessage());
        }
        $this->success('修改成功');
    }

    public function maxOrder()
    {
        $order = SysConfigModel::max('order');
        $this->success('', '', $order);
    }
}
